package com.groupfive.controller;

import com.github.pagehelper.PageInfo;
import com.groupfive.common.Result;
import com.groupfive.pojo.Account;
import com.groupfive.pojo.Admin;
import com.groupfive.service.AdminService;
import com.groupfive.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RefreshScope
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Value("${token.timeout}")
    private  Integer timeout;

    /**
     * 添加
     */
    @PostMapping("/add")
    public Result add(@RequestBody Admin admin){
        int count = adminService.add(admin);
        if (count == 0 ){
            return Result.error();
        }
        return Result.success();
    }

    /**
     * 更新数据
     */
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin){
        int count = adminService.update(admin);
        if (count == 0 ){
            return Result.error();
        }
        return Result.success();
    }

    /**
     * 删除数据
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        int count = adminService.deleteById(id);
        if (count == 0 ){
            return Result.error();
        }
        return Result.success();
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids){
       adminService.deleteBatch(ids);
       return Result.success();
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Admin admin){
        List<Admin> list = adminService.selectAll(admin);

        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id")Long id){
        Admin admin = adminService.selectById(id);
        return Result.success(admin);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Admin admin,
                             @RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize){
        PageInfo<Admin> pageInfo = adminService.selectPage(admin, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @PostMapping("/login")
    public Result login(@RequestBody Account account){
        System.out.println("token有效时间：" +  timeout + "小时");
        Account result = adminService.login(account);
        String token = JwtUtils.createToken(result.getId().toString(),timeout);
        result.setToken(token);
        return Result.success(result);
    }

    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account){
        adminService.updatePassword(account);
        return Result.success();
    }



}
